from odoo import models, fields, api, _
from odoo.exceptions import UserError, ValidationError


# 审批流类
class Workflow(models.Model):
    _inherit = 'sb.workflow'

    # 关联销售单据
    development_id = fields.Many2one('s.research.and.development.order', string="Related Documents", ondelete="cascade")

    workflow_type = fields.Selection(selection_add=[
        ('development', _('Development')),
        ('presets', _('Presets BOM'))  # 预设BOM
    ], ondelete={'activity': 'cascade'})

    confirm_date = fields.Datetime('Confirm date', related='development_id.confirm_date')
    planned_receiving_date = fields.Datetime('Planned delivery date', related='development_id.planned_receiving_date')
    dev_qty_text = fields.Char('Product Qty', compute="_get_qty_text", default="0")
    # ========= 预设BOM =========
    presets_id = fields.Many2one('sb.presets.bom', string="presets BOM")
    bom_line_ids = fields.One2many(related="presets_id.bom_line_ids", string="presets BOM line")
    presets_product_id = fields.Many2one(related="presets_id.product_id", string="MPN")
    presets_sheet_type = fields.Selection(related="presets_id.sheet_type", string="拼版")
    presets_sheet_multi = fields.Boolean(related="presets_id.sheet_multi", string="多拼版")
    presets_pnl_width = fields.Float(related="presets_id.pnl_width", string="PANEL 纬向")
    presets_pnl_length = fields.Float(related="presets_id.pnl_length", string="PANEL 经向")
    presets_pnl = fields.Float(related="presets_id.pnl", string="PNL/SHEET")
    presets_set_pnl = fields.Float(related="presets_id.set_pnl", string="SET/PNL")
    presets_pcs_set = fields.Float(related="presets_id.pcs_set", string="PCS/SET")
    presets_thickness = fields.Float(related="presets_id.thickness", string="外层完成铜厚")
    sheet_radial = fields.Float(related="presets_id.sheet_radial", string="sheet radial")
    sheet_zonal = fields.Float(related="presets_id.sheet_zonal", string="sheet zonal")
    layer = fields.Integer(related="presets_id.layer", string="Layer")
    contact = fields.Char(related="presets_id.contact", string="联系人/电话")

    @api.depends('development_id')
    def _get_qty_text(self):
        for row in self:
            if row.filtered(lambda x: x.workflow_type == 'development'):
                row.dev_qty_text = '/'.join([':'.join(x) for x in row.development_id.line_ids.mapped(
                    lambda x: (x.product_id.name, str(int(x.product_qty))))])
            else:
                row.dev_qty_text = '0'

# # 审批流类
# class WorkflowItem(models.Model):
#     _inherit = 'sb.workflow.item'
#
#     def review(self):
#         qty_available = 0
#         free_qty = 0
#         incoming_qty = 0
#         rec = super(WorkflowItem, self).review()
#         if rec['context'].get('workflow_type', '') == 'sale' and rec['context'].get('is_plan', False):
#             qty_available = self.sb_workflow_id.product_template_id.product_variant_id.qty_available
#             free_qty = self.sb_workflow_id.product_template_id.product_variant_id.free_qty
#             incoming_qty = self.sb_workflow_id.product_template_id.product_variant_id.incoming_qty
#             planned_receiving_date = self.sb_workflow_id.sale_order_id.planned_receiving_date
#             rec['context'].update(planned_receiving_date=planned_receiving_date)
#         rec['context'].update({
#             'qty_available': qty_available,
#             'free_qty': free_qty,
#             'incoming_qty': incoming_qty,
#             'product_template_id': self.sb_workflow_id.product_template_id.id
#         })
#         return rec
